package com.maxio;

import com.maxio.utils.SocketUtil;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.Bean;

@Slf4j
@ServletComponentScan	//开启了对servlet组件的支持
@SpringBootApplication
@MapperScan("com.maxio.mapper")
public class TliasWebManagenmentApplication {

	@Value("${server.port:8080}")
	private Integer webPort;

	public static void main(String[] args) {
		SpringApplication.run(TliasWebManagenmentApplication.class, args);
		System.out.println("http://localhost:8080/doc.html#/home");
	}

	/**
	 * 兼容本地启动时8080端口被占用的场景; 只有dev启动方式才做这个逻辑.
	 */
	@Bean
//    @ConditionalOnExpression(value = "#{'dev'.equals(environment.getProperty('env.name'))}")
	public TomcatConnectorCustomizer customServerPortTomcatConnectorCustomizer() {
		// 开发环境时，首先判断8080d端口是否可用；若可用则直接使用，否则选择一个可用的端口号启动
		int port = SocketUtil.findAvailableTcpPort(8000, 10000, webPort);
		if (port != webPort) {
			log.info("默认端口号{}被占用，随机启用新端口号: {}", webPort, port);
			webPort = port;
		}
		return connector -> connector.setPort(port);
	}

}
